import re
SC_file = "messagecompress.hpp"
f_vlog = open(SC_file, 'r')
ast_code  = open(SC_file[:-4]+".txt", 'w')



regx_MODULE = re.compile("^SC_MODULE\((.*)\)")
regx_signal = re.compile("\tsc_((\w*)(.*>)) (.*) *;")
regx_signal1 = re.compile(" *sc_((\w*)(.*>)) (.*) *;") ##制表符\t与四个空格不等价
regx_number = re.compile("\d+")
regx_alwayssen = re.compile("\t\tSC_METHOD\(always_block.*\)")




always_sen=[]
dir_signal={}
b=0

for line in f_vlog.readlines():
    if b==1:

        always_sen.append(line)
        print(always_sen)
        b=0

    else:
        if regx_MODULE.match(line):
            match_obj=regx_MODULE.match(line)
            # print("ModuleDef: %s\n" %match_obj.group(1))
            ast_code.write("ModuleDef: %s\n" %match_obj.group(1))
        elif regx_signal.match(line):
            match_obj=regx_signal.match(line)
            ast_code.write("Decl:\n" )
            # print("%s"%match_obj.group(2))
            if("%s"%match_obj.group(2)=="in"):

                if("," in ("%s"%match_obj.group(4))):
                    a = re.split(",", "%s" % match_obj.group(4))
                    for i in range(0,len(a)):
                        ast_code.write("Input: %s\n" % a[i])
                        if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                            num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                            max = int(num.group()) - 1
                            # print(num.group())
                            ast_code.write("Width:\n")
                            ast_code.write("IntConst: %s\n" % max)
                            # print("IntConst: %s\n"%max)
                            ast_code.write("IntConst: 0\n")
                        # print("Input: %s\n" % a[i])

                else:
                    ast_code.write("Input: %s\n" % match_obj.group(4))


                    # if ("%s"%match_obj.group(3)).isdigit():
                    if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                        num=re.search(r'\d+', "%s\n" % match_obj.group(3))
                        max=int(num.group())-1
                        # print(num.group())
                        ast_code.write("Width:\n")
                        ast_code.write("IntConst: %s\n"%max)
                        # print("IntConst: %s\n"%max)
                        ast_code.write("IntConst: 0\n")
                    # print("Input: %s\n" % match_obj.group(3))

            if ("%s"%match_obj.group(2)=="out"):
                if ("," in ("%s" % match_obj.group(4))):
                    a = re.split(",", "%s" % match_obj.group(4))
                    for i in range(0, len(a)):
                        ast_code.write("Output: %s\n" % a[i])
                        if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                            num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                            max = int(num.group()) - 1
                            # print(num.group())
                            ast_code.write("Width:\n")
                            ast_code.write("IntConst: %s\n" % max)
                            # print("IntConst: %s\n"%max)
                            ast_code.write("IntConst: 0\n")
                        # print("Input: %s\n" % a[i])

                else:
                    ast_code.write("Output: %s\n" % match_obj.group(4))
                    # print("%s\n" % match_obj.group(3))

                    # if ("%s"%match_obj.group(3)).isdigit():
                    if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                        num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                        max = int(num.group()) - 1
                        # print(num.group())
                        ast_code.write("Width:\n")
                        ast_code.write("IntConst: %s\n" % max)
                        # print("IntConst: %s\n"%max)
                        ast_code.write("IntConst: 0\n")
            if ("%s" % match_obj.group(2) == "signal"):
                # print("%s\n" % match_obj.group(4))

                if ("," in ("%s" % match_obj.group(4))):
                    a = re.split(",", "%s" % match_obj.group(4))
                    for i in range(0, len(a)):
                        if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                            num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                            max = int(num.group()) - 1
                            dir_signal["%s"%a[i]]=max

                else:
                    ast_code.write("Output: %s\n" % match_obj.group(4))
                    # print("%s\n" % match_obj.group(3))

                    # if ("%s"%match_obj.group(3)).isdigit():
                    if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                        num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                        max = int(num.group()) - 1
                        dir_signal["%s" % match_obj.group(4)]=max
            else :
                pass

        elif regx_signal1.match(line):
            match_obj=regx_signal1.match(line)
            ast_code.write("Decl:\n" )
            # print("%s"%match_obj.group(2))
            if("%s"%match_obj.group(2)=="in"):

                if("," in ("%s"%match_obj.group(4))):
                    a = re.split(",", "%s" % match_obj.group(4))
                    for i in range(0,len(a)):
                        ast_code.write("Input: %s\n" % a[i])
                        if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                            num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                            max = int(num.group()) - 1
                            # print(num.group())
                            ast_code.write("Width:\n")
                            ast_code.write("IntConst: %s\n" % max)
                            # print("IntConst: %s\n"%max)
                            ast_code.write("IntConst: 0\n")
                        # print("Input: %s\n" % a[i])

                else:
                    ast_code.write("Input: %s\n" % match_obj.group(4))


                    # if ("%s"%match_obj.group(3)).isdigit():
                    if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                        num=re.search(r'\d+', "%s\n" % match_obj.group(3))
                        max=int(num.group())-1
                        # print(num.group())
                        ast_code.write("Width:\n")
                        ast_code.write("IntConst: %s\n"%max)
                        # print("IntConst: %s\n"%max)
                        ast_code.write("IntConst: 0\n")
                    # print("Input: %s\n" % match_obj.group(3))

            if ("%s"%match_obj.group(2)=="out"):
                if ("," in ("%s" % match_obj.group(4))):
                    a = re.split(",", "%s" % match_obj.group(4))
                    for i in range(0, len(a)):
                        ast_code.write("Output: %s\n" % a[i])
                        if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                            num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                            max = int(num.group()) - 1
                            # print(num.group())
                            ast_code.write("Width:\n")
                            ast_code.write("IntConst: %s\n" % max)
                            # print("IntConst: %s\n"%max)
                            ast_code.write("IntConst: 0\n")
                        # print("Input: %s\n" % a[i])

                else:
                    ast_code.write("Output: %s\n" % match_obj.group(4))
                    # print("%s\n" % match_obj.group(3))

                    # if ("%s"%match_obj.group(3)).isdigit():
                    if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                        num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                        max = int(num.group()) - 1
                        # print(num.group())
                        ast_code.write("Width:\n")
                        ast_code.write("IntConst: %s\n" % max)
                        # print("IntConst: %s\n"%max)
                        ast_code.write("IntConst: 0\n")
            if ("%s" % match_obj.group(2) == "signal"):
                # print("%s\n" % match_obj.group(4))

                if ("," in ("%s" % match_obj.group(4))):
                    a = re.split(",", "%s" % match_obj.group(4))
                    for i in range(0, len(a)):
                        if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                            num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                            max = int(num.group()) - 1
                            dir_signal["%s"%a[i]]=max

                else:
                    ast_code.write("Output: %s\n" % match_obj.group(4))
                    # print("%s\n" % match_obj.group(3))

                    # if ("%s"%match_obj.group(3)).isdigit():
                    if bool(re.search(r'\d+', "%s\n" % match_obj.group(3))):
                        num = re.search(r'\d+', "%s\n" % match_obj.group(3))
                        max = int(num.group()) - 1
                        dir_signal["%s" % match_obj.group(4)]=max
            else :
                pass

        elif regx_alwayssen.match(line):
            b=1


        else:
            pass


print(dir_signal)